'use strict';
	angular.module('inventory.recordDetailInvoiceSelectMulti', [])
	.controller('recordDetailInvoiceSelectMultiCtrl'
			,['$rootScope', '$scope', 'BizData', '$uibModalInstance','params', 'title'
			   , f])//控制器结束
    function f($rootScope, $scope, BizData, $uibModalInstance, params, title) {


		var Model = this;
		

		Model.gridRowClick = function(event){
		}

		Model.gridRowDblClick = function(event){
			Model.ok();
		}
		
		var options = {};
		options.container = Model;
		options.concept = 'StorageRecordDetail';
		options.name = 'dataList';
		options.httpUrl = $rootScope.getServiceName();
		options.reader = '/inventory/recordDetailQuery/list/query';
		options.writer = '';
		options.deleter = '';
		options.idColumnDefine = "fID";
		options.idColumnName = "fID";
		options.isTree = false;
		options.parentRelation = 'fParent';
		options.nodeKindRelation = 'nodeKind';
		options.nodeLevelRelation = 'nodeLevel';
		options.pathRelation = '';
		options.master = {"data":"","relation":""};
		options.directDeleteMode = false;
		options.orderBy = 'recordNo,serialNo ASC';
		options.limit = 10;
		
		options.onIndexChanging = null;
		options.onIndexChanged = Model.dataListIndexChanged;
		options.onBeforeNew = Model.dataListBeforeNew;
		options.onAfterNew = Model.dataListAfterNew;
		options.onBeforeDelete = Model.dataListBeforeDelete;
		options.onAfterDelete = Model.dataListAfterDelete;
		options.onValueChanged = Model.dataListValueChanged;
		options.onDataChanged = Model.dataListDataChanged;
		
		$scope.dataList = new BizData(options);

		//////////////////////对话框使用开始//////////////////////////////////////
		
        //模态窗口打开之后执行的事件
        $uibModalInstance.opened.then(function(){
    		//接收调用对话框的参数
        	Model.title = title;  
        	Model.params = params;  

    		//设置过滤条件
    		$scope.dataList.filter = "StorageRecordDetail.status='open' "
    		+" AND StorageRecordDetail.audit='confirm' "
    		+" AND (StorageRecordDetail.priceAmount-StorageRecordDetail.invoiceQuantity-StorageRecordDetail.invoiceQuantitying)>0 "
    		
    		;
    		
//    		console.log('Model.params.filter-->',Model.params.filter);
    		if(Model.params.filter!=undefined && Model.params.filter!=''){
    			$scope.dataList.filter = $scope.dataList.filter + " AND " + Model.params.filter;
    		}

    		//刷新数据
    	    $scope.dataList.refreshData();
        });  
		
        Model.ok = function () {  
	    	//保存成功
	    	//打包数据
	    	var event = {};
	    	var data = [];
	    	
	    	//循环data
	    	for(var index=0;index<$scope.dataList.rows.length;index++){
	    		if($scope.dataList.rows[index].userdata.checked){
	    	    	
	    	    	var sourceRow = $scope.dataList.rows[index];
	    	    	var row = {};
	    			//循环行中的列，取出列的值，装配成简单的行
	    			for(var obj in sourceRow){
	    				if(obj!='$$hashKey' && obj!='userdata'){
	    					row[obj] = sourceRow[obj].value;
	    				}
	    			}
	    	    	
	    	    	data.push(row);
	    			
	    		}
	    	}
	    	
	    	event.data = data;
			$uibModalInstance.close(event);  
	    };  
	    
	    Model.cancel = function () {  
	        $uibModalInstance.dismiss('cancel');  
	    };    

	  //表格标题中的选择框ng-model
	  $scope.checkboxValue = false;
	  //表格标题中的选择对话框click事件
	  Model.checkboxClick = function () {  
	  	for(var index=0;index<$scope.dataList.rows.length;index++){
	  		$scope.dataList.rows[index].userdata.checked=$scope.checkboxValue;
	  	}
	  };    

	  //////////////////////对话框使用结束//////////////////////////////////////
	    



	    return Model;
	    
	}	
	
